/**
 * @author Caius
 * @description 定义股票缓存的实现
 * @version 1.0
 * @since Created in 2025-02-26
 */

package com.itsense.stock.face.Impl;

import com.itsense.stock.face.StockCacheFace;
import com.itsense.stock.mapper.StockBusinessMapper;
import com.itsense.stock.pojo.entity.StockBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component("StockCacheFace")
public class StockCacheFaceImpl implements StockCacheFace {
    @Autowired
    private StockBusinessMapper stockBusinessMapper;

    @Cacheable(cacheNames = "stock", key = "'stockCodes'")
    @Override
    public List<String> getAllStockCodeWithPrefix() {
        List<String> allStockCodes = stockBusinessMapper.getAllStockCodes();
        allStockCodes =  allStockCodes.stream().map(code->code.startsWith("6")?"sh"+code:"sz"+code).collect(Collectors.toList());
        return allStockCodes;
    }

    @CacheEvict(key="'getAllStockCodeWithPrefix'")
    @Override
    public void updateStockInfoById(StockBusiness info){
        stockBusinessMapper.updateByPrimaryKey(info);
    }
}
